package com.choosefine.it.mobile.equipment.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.HttpServiceBean;
import com.choosefine.it.base.dto.DateDTO;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.json.JsonUtil;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.mobile.equipment.dto.ShoppingBuyGatherDTO;
import com.choosefine.it.mobile.equipment.dto.ShoppingIdentityDTO;
import com.choosefine.it.mobile.equipment.dto.ShoppingLeaseGatherDTO;
import com.choosefine.it.mobile.equipment.vo.BuyGatherVO;
import com.choosefine.it.mobile.equipment.vo.EquipmentReceiptVO;
import com.choosefine.it.mobile.equipment.vo.LeaseEquipmentAndSumVO;
import com.choosefine.it.mobile.equipment.vo.LeaseEquipmentDetailAndSumVO;
import com.choosefine.it.mobile.equipment.vo.LeaseEquipmentDetailSumVO;
import com.choosefine.it.mobile.equipment.vo.LeaseEquipmentDetailVO;
import com.choosefine.it.mobile.equipment.vo.LeaseEquipmentSumVO;
import com.choosefine.it.mobile.equipment.vo.LeaseEquipmentVO;
import com.choosefine.it.mobile.equipment.vo.LeaseGatherVO;
import com.choosefine.it.mobile.equipment.vo.ShopTransactionAmountMonthListVO;
import com.choosefine.it.mobile.equipment.vo.ShopTransactionAmountVO;
import com.choosefine.it.mobile.equipment.vo.ShopVO;
import com.choosefine.it.mobile.equipment.vo.ShoppingBuyGatherVO;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipment;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipmentAndSum;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipmentDetail;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipmentDetailAndSum;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipmentDetailAndSumAndReceipt;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipmentDetailSum;
import com.choosefine.it.mobile.equipment.vo.ShoppingEquipmentVO;
import com.choosefine.it.mobile.equipment.vo.ShoppingLeaseGatherVO;
import com.choosefine.it.mobile.utils.JavabeanConvertUrl;
import com.google.gson.Gson;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;
@RestController
@Api(value="APP-物料-商城",description="陈建|董自齐")
@RequestMapping("/app/shoppingEquipment")
public class APPShoppingEquipmentController extends BaseController{
	
	@Autowired
	private HttpServiceBean httpServiceBean;
	
	@Value("#{yaml.get('shopping.order.head')}")
	private String orderHead;//订单
	@Value("#{yaml.get('shopping.order.itemDetail')}")
	private String itemDetail;//购买详细
	@Value("#{yaml.get('shopping.order.shop')}")
	private String orderShop;//商家
	//获取采购订单汇总
	@Value("#{yaml.get('shopping.order.materiel')}")
	private String materielURL;
	@Value("#{yaml.get('shopping.order.leaseDetail')}")
	private String leaseDetail;//租赁详细
	//获取租赁订单汇总
	@Value("#{yaml.get('shopping.order.leaseMateriel')}")
	private String leaseMaterielURL;
	@Value("#{yaml.get('shopping.order.payableCount')}")
	private String orderPayableCount;//订单应付金额统计
	@Value("#{yaml.get('shopping.order.paidCount')}")
	private String orderPaidCount;//订单已付金额
	@Value("#{yaml.get('shopping.order.total')}")
	private String orderTotal;
	@Value("#{yaml.get('shopping.order.totalDetail')}")
	private String totalDetail;
	@Value("#{yaml.get('shopping.order.shopCount')}")
	private String shopCount;
	//获取采购订单汇总累计金额
	@Value("#{yaml.get('shopping.order.materielCount')}")
	private String materielCountURL;
	//获取租赁订单汇总累计天数、金额
	@Value("#{yaml.get('shopping.order.receptionMaterielCount')}")
	private String receptionMaterielCountURL;
	@Value("#{yaml.get('shopping.order.quantity')}")
	private String orderQuantity;
	@Value("#{yaml.get('shopping.order.equipmentTypeList')}")
	private String equipmentTypeList;
	
	@Autowired
	private CustomRestTemplate restTemplate;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value="获取购买类型订单集合（商城）",response=ShoppingEquipmentAndSum.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：1材料2设施3设备",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="categoryId",value="商品种类名",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="时间年-月",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要总数：1为需要，2为不需要，默认为2",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行",defaultValue="",required=false,dataType="Integer",paramType="query")
	})
	@RequestMapping(value="/shoppingTypeList",method=RequestMethod.GET)
	public String shoppingTypeList(
			ShoppingIdentityDTO identity,
			@RequestParam(value="shopId",defaultValue="") Integer shopId,
			@RequestParam(value="merchandiseType",defaultValue="")Integer merchandiseType,
			@RequestParam(value="categoryId",defaultValue="")Integer categoryId,
			@RequestParam(value="date",defaultValue="") String date,
			@RequestParam(value="isSum",defaultValue="2") Integer isSum,
			PageDTO page) throws Exception{
		Map<String, Object> map=new HashMap<String,Object>();//装参
		map.put("teamId",identity.getTeamId() );
		map.put("shopId",shopId );
		map.put("merchandiseType",merchandiseType );
		map.put("categoryId",categoryId );
		map.put("projectCode",identity.getProjectCode() );
		map.put("buyType",1);
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//定义前端传入时间格式
		StringBuffer materielUrl=URLHandlerUtils.getUrlBuffer(shoppingURL,  materielURL);//购买商品大类列表
		ShoppingEquipmentAndSum shoppingAndSum=new ShoppingEquipmentAndSum();
		httpServiceBean.shopUrlFormat(materielUrl, map, page, dateDto);//生成url
		String materieResult=httpServiceBean.getRequestBody(materielUrl.toString(), null);//查找大类list结果
		Gson gson=new Gson();
		String jsonList=httpServiceBean.resultJsonFormat(materieResult, CommonConstant.DATA_KEY,CommonConstant.LIST_KEY).toString();
		if("".equals(jsonList)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		List<Map> ses=gson.fromJson(jsonList,List.class);
		List<ShoppingEquipment> resultList=new ArrayList<ShoppingEquipment>();
		for (Map mapp : ses) {
			ShoppingEquipment now = gson.fromJson(gson.toJson(mapp),ShoppingEquipment.class);
			resultList.add(now);
		}
		shoppingAndSum.setList(resultList);
		
		if(isSum==1){//==1则为需要总数
			StringBuffer materielCountUrl= URLHandlerUtils.getUrlBuffer(shoppingURL,  materielCountURL);//购买商品总额
			httpServiceBean.shopUrlFormat(materielCountUrl, map, null, dateDto);//生成url
			String materielCountResult=httpServiceBean.getRequestBody(materielCountUrl.toString(), null);//查找结果
			JSONObject js=new JSONObject(materielCountResult);
			shoppingAndSum.setMaterieSum(js.getDouble(CommonConstant.DATA_KEY));
		}
		return responseSuccess(shoppingAndSum);
		
	}
	
	@ApiOperation(value="获取购买类型订单详细集合（商城）",response=ShoppingEquipmentDetailAndSumAndReceipt.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：1材料2设施3设备",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="categoryId",value="商品种id",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="时间年-月",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要总数：1为需要，2为不需要，默认为2",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行",defaultValue="",required=false,dataType="Integer",paramType="query")
	})
	@RequestMapping(value="/shoppingTypeDetail",method=RequestMethod.GET)
	public String shoppingTypeDetail(
			ShoppingIdentityDTO identity,
			@RequestParam(value="shopId",defaultValue="") Integer shopId,
			@RequestParam(value="merchandiseType",defaultValue="")Integer merchandiseType,
			@RequestParam(value="categoryId",defaultValue="")Integer categoryId,
			@RequestParam(value="date",defaultValue="") String date,
			@RequestParam(value="isSum",defaultValue="2") Integer isSum,
			PageDTO page) throws Exception{
		Map<String, Object> map=new HashMap<String,Object>();//装参
		map.put("teamId",identity.getTeamId() );
		map.put("shopId",shopId );
		map.put("merchandiseType",merchandiseType );
		map.put("categoryId",categoryId);
		map.put("projectCode",identity.getProjectCode());
		map.put("buyType",1);
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//定义前端传入时间格式
		
		if(merchandiseType==1){//查看材料多出到货数量，返回与查找单写方法
			ShoppingEquipmentDetailAndSumAndReceipt result=new ShoppingEquipmentDetailAndSumAndReceipt();
			if(!materialShoppingDetail(map, page, dateDto, isSum, result)){
				return responseSuccess();
			};
			return responseSuccess(result);
		}
		ShoppingEquipmentDetailAndSum result=new ShoppingEquipmentDetailAndSum();
		if(!equipmentShoppingDetail(map, page, dateDto, isSum, result)){
			return responseSuccess();
		};
		return responseSuccess(result);
	}
	
	/**
	 * 12.17dzq 材料查看详情方法 
	 */
	@SuppressWarnings({ "rawtypes", "unchecked"})
	private boolean materialShoppingDetail(Map<String,Object> map,PageDTO page,DateDTO date,Integer isSum,ShoppingEquipmentDetailAndSumAndReceipt result)
	throws Exception{
		StringBuffer itemDetailUrl=URLHandlerUtils.getUrlBuffer(shoppingURL,  itemDetail);//购买商品大类列表
		httpServiceBean.shopUrlFormat(itemDetailUrl, map, page, date);//生成url
		String itemDetailResult=httpServiceBean.getRequestBody(itemDetailUrl.toString(), null);//查找大类list结果
		Gson gs=new Gson();//定义gs对象
		String gsString=httpServiceBean.resultJsonFormat(itemDetailResult, CommonConstant.DATA_KEY,CommonConstant.LIST_KEY).toString();//取出list字符串
		if("".equals(gsString)){
			return false;//啥也没有，不需要继续下去。
		}
		List<Map> list= gs.fromJson(gsString, List.class);//将字符串转化为list
		List<ShoppingEquipmentDetail> resultList=new ArrayList<ShoppingEquipmentDetail>();//定义返回list
		for(Map mapNow:list){//循环将每一个list 的map类型转化为适用类型，并添到返回集合
			ShoppingEquipmentDetail now=gs.fromJson(gs.toJson(mapNow), ShoppingEquipmentDetail.class);
			resultList.add(now);
		}
		if(isSum==1){//==1则为需要总数
			StringBuffer materielUrl=URLHandlerUtils.getUrlBuffer(shoppingURL,  materielURL);//购买商品总额
			httpServiceBean.shopUrlFormat(materielUrl, map, null, date);//生成url
			String materielCountResult=httpServiceBean.getRequestBody(materielUrl.toString(), null);//查找结果
			String materielCont=httpServiceBean.resultJsonFormat(materielCountResult,CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
			List<Map> materielContList= gs.fromJson(materielCont, List.class);
			ShoppingEquipmentDetailSum seds=gs.fromJson(gs.toJson(materielContList.get(0)), ShoppingEquipmentDetailSum.class);//只会有一条，所以只取单个
			result.setDetailSum(seds);
			//到货数量
			
			StringBuffer quantityUrl = URLHandlerUtils.getUrlBuffer(shoppingURL,  orderQuantity);//定义url
			httpServiceBean.shopUrlFormat(quantityUrl, map, page, date);//拼接参数
			String quantityResult=httpServiceBean.getRequestBody(quantityUrl.toString(), null);//取出收货数量
			String quantity=httpServiceBean.resultJsonFormat(quantityResult, CommonConstant.DATA_KEY).toString();//取出date
			EquipmentReceiptVO receiptVO=gs.fromJson(quantity,EquipmentReceiptVO.class );//转换为对象
			result.setReceipt(receiptVO);//放入对象
		}
		result.setList(resultList);//添加入返回对象
		return true;
	}
	
	/**
	 * 12.17dzq 设备，设施查看详情方法
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private boolean equipmentShoppingDetail(Map<String,Object> map,PageDTO page,
			DateDTO date,Integer isSum,ShoppingEquipmentDetailAndSum result)throws Exception{
		StringBuffer itemDetailUrl=URLHandlerUtils.getUrlBuffer(shoppingURL,  itemDetail);//购买商品大类列表
		httpServiceBean.shopUrlFormat(itemDetailUrl, map, page, date);//生成url
		String itemDetailResult=httpServiceBean.getRequestBody(itemDetailUrl.toString(), null);//查找大类list结果
		Gson gs=new Gson();//定义gs对象
		String gsString=httpServiceBean.resultJsonFormat(itemDetailResult,CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();//取出list字符串
		if("".equals(gsString)){
			return false;//啥也没有，不需要继续下去。
		}
		List<Map> list= gs.fromJson(gsString, List.class);//将字符串转化为list
		List<ShoppingEquipmentDetail> resultList=new ArrayList<ShoppingEquipmentDetail>();//定义返回list
		for(Map mapNow:list){//循环将每一个list 的map类型转化为适用类型，并添到返回集合
			ShoppingEquipmentDetail now=gs.fromJson(gs.toJson(mapNow), ShoppingEquipmentDetail.class);
			resultList.add(now);
		}
		if(isSum==1){//==1则为需要总数
			StringBuffer materielUrl=URLHandlerUtils.getUrlBuffer(shoppingURL,  materielURL);//购买商品总额
			httpServiceBean.shopUrlFormat(materielUrl, map, null, date);//生成url
			String materielCountResult=httpServiceBean.getRequestBody(materielUrl.toString(), null);//查找结果
			String materielCont=httpServiceBean.resultJsonFormat(materielCountResult,CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
			List<Map> materielContList= gs.fromJson(materielCont, List.class);
			ShoppingEquipmentDetailSum seds;
			if(materielContList.size()>0)
				seds=gs.fromJson(gs.toJson(materielContList.get(0)), ShoppingEquipmentDetailSum.class);//只会有一条，所以只取单个
			else
				seds=new ShoppingEquipmentDetailSum();
			result.setDetailSum(seds);
		}
		result.setList(resultList);//添加入返回对象
		return true;
	}

	/**
	 * 12.16dzq 租赁订单集合
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value="获取租赁类型订单集合（商城）",response=LeaseEquipmentAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：2设施3设备",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="categoryId",value="商品种类Id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="时间年-月",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要总数：1为需要，2为不需要，默认为2",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行",defaultValue="",required=false,dataType="Integer",paramType="query")
	})
	@RequestMapping(value="/leaseTypeList",method=RequestMethod.GET)
	public String leaseTypeList(
								ShoppingIdentityDTO identity,
								@RequestParam(value="shopId",defaultValue="") Integer shopId,
								@RequestParam(value="merchandiseType",defaultValue="")Integer merchandiseType,
								@RequestParam(value="categoryId",defaultValue="")Integer categoryId,
								@RequestParam(value="date",defaultValue="") String date,
								@RequestParam(value="isSum",defaultValue="2") Integer isSum,
								PageDTO page) throws Exception{
		Map<String, Object> map=new HashMap<String,Object>();//装参
		map.put("teamId",identity.getTeamId() );
		map.put("shopId",shopId );
		map.put("merchandiseType",merchandiseType );
		map.put("categoryId",categoryId );
		map.put("projectCode",identity.getProjectCode() );
		map.put("buyType",2);
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//定义前端传入时间格式
		StringBuffer leaseMaterielUrl= URLHandlerUtils.getUrlBuffer(shoppingURL,  leaseMaterielURL);//租赁商品大类列表
		httpServiceBean.shopUrlFormat(leaseMaterielUrl, map, page, dateDto);//生成url
		String leaseMaterielResult=httpServiceBean.getRequestBody(leaseMaterielUrl.toString(), null);//查找大类list结果
		LeaseEquipmentAndSumVO result=new LeaseEquipmentAndSumVO();
		Gson gs=new Gson();
		String gsString=httpServiceBean.resultJsonFormat(leaseMaterielResult, CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();//取出list字符串
		if("".equals(gsString)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		List<Map> listResult= gs.fromJson(gsString, List.class);
		List<LeaseEquipmentVO> list=new ArrayList<LeaseEquipmentVO>();
		for(Map mapNow:listResult){//循环将每一个list 的map类型转化为适用类型，并添到返回集合
			LeaseEquipmentVO now=gs.fromJson(gs.toJson(mapNow), LeaseEquipmentVO.class);
			list.add(now);
		}
		result.setList(list);
		if(isSum==1){//==1则为需要总数
			StringBuffer materielCountUrl= URLHandlerUtils.getUrlBuffer(shoppingURL,  receptionMaterielCountURL);//租赁商品总额
			httpServiceBean.shopUrlFormat(materielCountUrl, map, null, dateDto);//生成url
			String materielCountResult=httpServiceBean.getRequestBody(materielCountUrl.toString(), null);//查找结果
			String gsSumResult=httpServiceBean.resultJsonFormat(materielCountResult,CommonConstant.DATA_KEY).toString();//定义结果集合
			LeaseEquipmentSumVO sum=gs.fromJson(gsSumResult, LeaseEquipmentSumVO.class);
			result.setSum(sum);
		}
		return responseSuccess(result);
		
	}
	
	
	/**
	 * 12.16dzq 租赁订单详细集合
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value="获取租赁类型订单详细集合（商城）",response=LeaseEquipmentDetailAndSumVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：2设施3设备",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="categoryId",value="商品种类Id",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="时间年-月",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="isSum",value="是否需要总数：1为需要，2为不需要，默认为2",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行",defaultValue="",required=false,dataType="Integer",paramType="query")
	})
	@RequestMapping(value="/leaseTypeListDetail",method=RequestMethod.GET)
	public String leaseTypeListDetail(
			ShoppingIdentityDTO identity,
			@RequestParam(value="shopId",defaultValue="") Integer shopId,
			@RequestParam(value="merchandiseType",defaultValue="")Integer merchandiseType,
			@RequestParam(value="categoryId",defaultValue="")Integer categoryId,
			@RequestParam(value="date",defaultValue="") String date,
			@RequestParam(value="isSum",defaultValue="2") Integer isSum,
			PageDTO page) throws Exception{
		Map<String, Object> map=new HashMap<String,Object>();//装参
		map.put("teamId",identity.getTeamId() );
		map.put("shopId",shopId );
		map.put("merchandiseType",merchandiseType );
		map.put("categoryId",categoryId );
		map.put("projectCode",identity.getProjectCode() );
		map.put("buyType",2);
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//定义前端传入时间格式
		StringBuffer leaseDetailUrl= URLHandlerUtils.getUrlBuffer(shoppingURL,  leaseDetail);//租赁商品大类列表
		httpServiceBean.shopUrlFormat(leaseDetailUrl, map, page, dateDto);//生成url
		String leaseDetailResult=httpServiceBean.getRequestBody(leaseDetailUrl.toString(), null);//查找大类list结果

		LeaseEquipmentDetailAndSumVO result=new LeaseEquipmentDetailAndSumVO();
		Gson gs=new Gson();
		String gsDetialResult =httpServiceBean.resultJsonFormat(leaseDetailResult,CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
		if("".equals(gsDetialResult)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		List<Map> resultList=gs.fromJson(gsDetialResult,List.class);
		List<LeaseEquipmentDetailVO> list=new ArrayList<LeaseEquipmentDetailVO>();
		for(Map nows:resultList){
			LeaseEquipmentDetailVO now=gs.fromJson(gs.toJson(nows),LeaseEquipmentDetailVO.class);
			list.add(now);
		}
		result.setList(list);
		if (isSum == 1) {// ==1则为需要总数
			StringBuffer leaseMaterielUrl=URLHandlerUtils.getUrlBuffer(shoppingURL,  leaseMaterielURL);//租赁商品总额
			httpServiceBean.shopUrlFormat(leaseMaterielUrl, map, null, dateDto);//生成url
			String leaseMaterielSumResult=httpServiceBean.getRequestBody(leaseMaterielUrl.toString(), null);//查找结果
			String gsSum=httpServiceBean.resultJsonFormat(leaseMaterielSumResult,CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
			List<Map> sumList=gs.fromJson(gsSum, List.class);
			LeaseEquipmentDetailSumVO sum;
			if(sumList.size()>0)
				sum=gs.fromJson(gs.toJson(sumList.get(0)), LeaseEquipmentDetailSumVO.class);
			else
				sum=new LeaseEquipmentDetailSumVO();
			result.setSum(sum);
		}
		return responseSuccess(result);
	}
	
	/***
	 * 12.10dzq 供应商交易金额列表
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value="供应商交易金额列表",response=ShopTransactionAmountVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：1材料2设施3设备",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",defaultValue="",required=false,dataType="Integer",paramType="query")
	})
	@GetMapping(value="/supplierAmount")
	public String supplier(
			@RequestParam(value="merchandiseType",defaultValue="")Integer merchandiseType,
			@RequestParam(value="shopId",defaultValue="")Integer shopId,
			ShoppingIdentityDTO identity,
			PageDTO page
			)throws Exception{
		
		StringBuffer url= URLHandlerUtils.getUrlBuffer(shoppingURL,  orderTotal);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId", identity.getTeamId());
		map.put("projectCode",identity.getProjectCode());
		map.put("merchandiseType", merchandiseType);
		map.put("shopId", shopId);
		httpServiceBean.shopUrlFormat(url, map, page, null);
		String result=httpServiceBean.getRequestBody(url.toString(), null);
		String resultList=httpServiceBean.resultJsonFormat(result, CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
		if("".equals(resultList)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		Gson gs=new Gson();
		List<Map> gsList=gs.fromJson(resultList, List.class);
		List<ShopTransactionAmountVO> list = new ArrayList<ShopTransactionAmountVO>(); 
		for(Map nows:gsList){
			ShopTransactionAmountVO now=gs.fromJson(gs.toJson(nows), ShopTransactionAmountVO.class);
			list.add(now);
		}
		return responseSuccess(list);
	}
	
	/***
	 * 12.10dzq 供应商交易金额二级列表
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value="供应商交易金额二级列表",response=ShopTransactionAmountMonthListVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="shopId",value="供应商id",defaultValue="",required=true,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页条",defaultValue="",required=false,dataType="Integer",paramType="query")
	})
	@RequestMapping(value="/supplierDetail",method=RequestMethod.GET)
	public String supplierDetail (
			@RequestParam(value="shopId",defaultValue="")Integer shopId,
			ShoppingIdentityDTO identity,
			PageDTO page
			)throws Exception{

		if(shopId==null||shopId==0){
			return responseFail("参数不合法");
		}
		StringBuffer url=URLHandlerUtils.getUrlBuffer(shoppingURL,  totalDetail);
		Map<String, Object> map= new HashMap<String, Object>();
		map.put("teamId", identity.getTeamId());
		map.put("projectCode", identity.getProjectCode());
		map.put("shopId", shopId);
		httpServiceBean.shopUrlFormat(url, map, page, null);
		String result=httpServiceBean.getRequestBody(url.toString(), null);
		String gsString=httpServiceBean.resultJsonFormat(result, CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
		if("".equals(gsString)){
			return responseSuccess();//啥也没有，不需要继续下去。
		}
		Gson gs=new Gson();
		List<Map> gsList=gs.fromJson(gsString, List.class);
		List<ShopTransactionAmountMonthListVO> list=new ArrayList<ShopTransactionAmountMonthListVO>();
		for(Map nows:gsList){
			ShopTransactionAmountMonthListVO now=gs.fromJson(gs.toJson(nows),ShopTransactionAmountMonthListVO.class);
			list.add(now);
		}
		return responseSuccess(list);
	}
	
	/***
	 * 12.5dzq 返回商家列表
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ApiOperation(value="返回交易过的商家列表(商城)")
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="shopName",value="商家名",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目Code",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="date",value="开始时间",defaultValue="",required=false,dataType="String",paramType="query")
	})
	
	@RequestMapping(value="/supplier",method=RequestMethod.GET)
	public String supplier (
			@RequestParam(value="shopName",defaultValue="") String shopName,
			ShoppingIdentityDTO identity,
			String date
			)throws Exception{
		if(identity.getTeamId()==null&&identity.getProjectCode()==null){
			return responseFail("参数不合法");
		}
		DateDTO dateDto=DateUtils.conversionDateDto(date, CommonConstant.DATE_FOMART);//定义前端传入时间格式
		StringBuffer url=URLHandlerUtils.getUrlBuffer(shoppingURL,  orderShop);
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("teamId", identity.getTeamId());
		map.put("projectCode", identity.getProjectCode());
		map.put("shopName", shopName);
		httpServiceBean.shopUrlFormat(url, map,null,dateDto);
		String result=httpServiceBean.getRequestBody(url.toString(), null);
		String listString=httpServiceBean.resultJsonFormat(result,CommonConstant.DATA_KEY, CommonConstant.LIST_KEY).toString();
		if("".equals(listString)){
			return responseSuccess();
		}
		Gson gs=new Gson();
		List<Map> gsList=gs.fromJson(listString, List.class);
		List<ShopVO> list=new ArrayList<ShopVO>();
		for(Map nows:gsList){
			ShopVO now=gs.fromJson(gs.toJson(nows), ShopVO.class);
			list.add(now);
		}
		return responseSuccess(list);
	}
	
	@ApiOperation("查找所有品类")
    @GetMapping("/type")
	@ApiImplicitParams({
		@ApiImplicitParam(name="categoryType",value="类别：1材料，2设施，3设备",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="typeName",value="类型名",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="pageNum",value="开始页",defaultValue="",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="结束行",defaultValue="",required=false,dataType="Integer",paramType="query"),
	})
    public String selectTypeList(
    		@RequestParam(value="categoryType",defaultValue="")Integer categoryType,
    		@RequestParam(value="typeName",defaultValue="")String typeName,
    		PageDTO page) throws Exception{
    	StringBuffer url= URLHandlerUtils.getUrlBuffer(shoppingURL,  equipmentTypeList);
    	Map<String, Object> map=new HashMap<String, Object>();
    	map.put("categoryType", categoryType);
    	map.put("names", typeName);
    	map.put("isleaf", 1);//商城默认参数，表示为所有子类  
    	httpServiceBean.shopUrlFormat(url, map, page, null);
    	String gsResult=httpServiceBean.getRequestBody(url.toString(), null);
    	return gsResult;
    }
	  
	@ApiOperation(value="根据projectCode和用户的usercode查询在该项目中该用户所下订单的供应商")
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目的projectCode",required=true,dataType="String",paramType="query"),
		@ApiImplicitParam(name="userCode",value="用户的userCode",required=false,dataType="String",paramType="query")
	})
	@RequestMapping(value="getShop",method=RequestMethod.GET)
	public String getShopByProjectCodeAndUserCode(String projectCode,String userCode){
		String result = null;
		StringBuffer urls= URLHandlerUtils.getUrlBuffer(shoppingURL,  orderHead);
		if (projectCode != null) {
			urls.append("projectCode=" + projectCode + "&");
		}
		if (userCode != null) {

			urls.append("userCode=" + userCode + "&");
		}
		String url = URLHandlerUtils.DefaultUriTemplateHandler(urls.toString());
		Map map = restTemplate.getForObject(url, Map.class, new HashMap<>());
		Map tfResult = (Map) map.get("data");
		List tfList = (List) tfResult.get("list");
		List<ShopVO> shopList = new ArrayList<>();
		if(tfList!=null){
			for(int i=0;i<tfList.size();i++){
				ShopVO sVo=new ShopVO();
				Map rMap=(Map) tfList.get(i);
				Integer shopId=Integer.valueOf(rMap.get("shopId").toString());
				String shopName=(String) rMap.get("shopName");
				sVo.setShopId(shopId);
				sVo.setShopName(shopName);
				shopList.add(sVo);
			}
			result=responseArraySuccess(shopList);
		}else{
			result =responseFail("暂无数据");
		}
		return result;
	}
	@ApiOperation(value="根据projectCode(如果是班组用户需要班组的id)来加载每个供应商提供的材料列表")
	@ApiImplicitParams({
		@ApiImplicitParam(name="projectCode",value="项目的projectCode",required=true,dataType="String",paramType="query"),
		@ApiImplicitParam(name="shopId",value="供应商的id（0表示加载全部不根据供应商的id查询）",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="teamId",value="班组用户的teamid",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="1材料2设施3设备",required=false,dataType="Integer",paramType="query"),
		@ApiImplicitParam(name="isLease",value="是否是租赁的设施设备（0表示不是租赁）",required=false,dataType="Integer",paramType="query"),
	})
	@RequestMapping(value="getByShop",method=RequestMethod.GET)
	public String selectByShop(Integer shopId,String projectCode,Integer teamId,Integer isLease,Integer merchandiseType){
		String result = null;
		StringBuffer urls= URLHandlerUtils.getUrlBuffer(shoppingURL,  materielURL);
		if (projectCode != null) {
			urls.append("projectCode=" + projectCode + "&");
		}
		if (shopId.intValue() != 0) {
			urls.append("shopId=" + shopId + "&");
		}
		if (teamId != null) {

			urls.append("teamId=" + teamId + "&");
		}
		if (merchandiseType != null) {

			urls.append("merchandiseType=" + merchandiseType + "&");
		}
		String url = URLHandlerUtils.DefaultUriTemplateHandler(urls.toString());
		Map map = restTemplate.getForObject(url, Map.class, new HashMap<>());
		Map tfResult = (Map) map.get("data");
		List tfList = (List) tfResult.get("list");
		List<ShoppingEquipmentVO> resultList=new ArrayList<ShoppingEquipmentVO>();
		ShoppingEquipmentVO seVo=null;
		if(tfList!=null){
			for(int i=0;i<tfList.size();i++){
				seVo=new ShoppingEquipmentVO();
				Map rMap=(Map) tfList.get(i);
				Integer categoryId=Integer.valueOf(rMap.get("categoryId").toString());
				String categoryName=(String) rMap.get("categoryName");
				String merchandiseUnit=(String) rMap.get("merchandiseUnit");
				Integer quantity =Integer.valueOf(rMap.get("quantity").toString());
				Double price=Double.valueOf(rMap.get("price").toString());
				if(isLease!=0){
				Integer adjustDays=Integer.valueOf(rMap.get("adjustDays").toString());
				seVo.setAdjustDays(adjustDays);
				}
				seVo.setCategoryId(categoryId);
				seVo.setCategoryName(categoryName);
				seVo.setMerchandiseUnit(merchandiseUnit);
				seVo.setPrice(price);
				seVo.setQuantity(quantity);
				resultList.add(seVo);
				}
			result=responseArraySuccess(resultList, ResultCode.SUCCESS, "查询成功");
		}else{
			result=responseFail("查询失败");
		}
		return result;
	}
	
	
	/**
	 * @author chenjian
	 * @time 2016年12月27日上午11:55:51
	 */
	@ApiIgnore
	@ApiOperation(value="获取采购订单汇总及汇总金额（陈健）",response=ShoppingBuyGatherVO.class)
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：1材料2设施3设备",defaultValue="",required=true,dataType="int",paramType="query"),
		@ApiImplicitParam(name="categoryId",value="商品种类名",defaultValue="",required=false,dataType="int",paramType="query"),
        @ApiImplicitParam(paramType = "query", name = "startTime",defaultValue="", dataType = "String", required = false, value = "开始时间"),
        @ApiImplicitParam(paramType = "query", name = "endTime",defaultValue="", dataType = "String", required = false, value = "结束时间"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行",defaultValue="",required=false,dataType="int",paramType="query")
	})
	@RequestMapping(value = "/buyOrderGatherList", method = RequestMethod.GET)
	public String getBuyOrderGatherList(ShoppingBuyGatherDTO  shoppingBuyGatherDTO) throws Exception{
		
		//订单汇总
		StringBuffer url = URLHandlerUtils.getUrlBuffer(shoppingURL,  materielURL);
		String requestURL=JavabeanConvertUrl.beanConvertUr(url, shoppingBuyGatherDTO);
		String result = restTemplate.getForObject(requestURL,  String.class);
		List<ShoppingBuyGatherVO> list = JsonUtil.jsonToArray(result, ShoppingBuyGatherVO.class);
		
		//订单金额汇总
		StringBuffer sumUrl = URLHandlerUtils.getUrlBuffer(shoppingURL,  materielCountURL);
		String requestSumURL=JavabeanConvertUrl.beanConvertUr(sumUrl, shoppingBuyGatherDTO);
		String sumResult = restTemplate.getForObject(requestSumURL,  String.class);
		Double totalPrice = (Double) JsonUtil.jsonGetValue(sumResult, "data");
		
		BuyGatherVO buyGatherVO = new BuyGatherVO();
		buyGatherVO.setShoppingBuyGatherVOList(list);
		buyGatherVO.setTotalPrice(totalPrice);
		return responseSuccess(buyGatherVO);
	}
	
	/**
	 * @author chenjian
	 * @time 2016年12月27日下午2:26:20
	 */
	@ApiIgnore
	@SuppressWarnings("unchecked")
	@ApiImplicitParams({
		@ApiImplicitParam(name="teamId",value="班组id",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="shopId",value="商家id",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="projectCode",value="项目code",defaultValue="",required=false,dataType="String",paramType="query"),
		@ApiImplicitParam(name="merchandiseType",value="订单类型id：1材料2设施3设备",defaultValue="",required=true,dataType="int",paramType="query"),
		@ApiImplicitParam(name="categoryId",value="商品种类名",defaultValue="",required=false,dataType="int",paramType="query"),
        @ApiImplicitParam(paramType = "query", name = "startTime",defaultValue="", dataType = "String", required = false, value = "开始时间"),
        @ApiImplicitParam(paramType = "query", name = "endTime",defaultValue="", dataType = "String", required = false, value = "结束时间"),
		@ApiImplicitParam(name="pageNum",value="起始页",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="pageSize",value="每页行",defaultValue="",required=false,dataType="int",paramType="query"),
		@ApiImplicitParam(name="buyType",value="1 for 购买, 2 for 租赁",defaultValue="2",required=true,dataType="int",paramType="query")
	})
	@ApiOperation(value="获取租赁订单汇总及汇总天数、金额（陈健）",response=ShoppingLeaseGatherVO.class)
	@RequestMapping(value = "/leaseOrderGatherList", method = RequestMethod.GET)
	public String getLeaseOrderGatherList(ShoppingLeaseGatherDTO  shoppingLeaseGatherDTO) throws Exception{
		//租赁订单汇总
		StringBuffer url = URLHandlerUtils.getUrlBuffer(shoppingURL,  leaseMaterielURL);
		String requestURL=JavabeanConvertUrl.beanConvertUr(url, shoppingLeaseGatherDTO);
		String result = restTemplate.getForObject(requestURL,  String.class);
		List<ShoppingLeaseGatherVO> list = JsonUtil.jsonToArray(result, ShoppingLeaseGatherVO.class);
		
		//租赁订单金额汇总
		StringBuffer sumUrl = URLHandlerUtils.getUrlBuffer(shoppingURL,  receptionMaterielCountURL);
		String requestSumURL=JavabeanConvertUrl.beanConvertUr(sumUrl, shoppingLeaseGatherDTO);
		String sumResult = restTemplate.getForObject(requestSumURL,  String.class);
		Map<String, Object> map = JsonUtil.jsonToMap(sumResult);
		
		LeaseGatherVO leaseGatherVO = new LeaseGatherVO();
		leaseGatherVO.setShoppingLeaseGatherVOList(list);
		leaseGatherVO.setTotalDays((Double) map.get("totalDays"));
		leaseGatherVO.setTotalPrice((Double) map.get("totalPrice"));
		return responseSuccess(leaseGatherVO);
	}

}
